package coffeebar;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.tree.*;

public class GUI extends JFrame {
	Grid items;
	Grid users;
	JPanel transactionListArea;
	JButton bCashout;
	JTree transactionList;
	JPanel mainArea;
	TransactionControls transactionControls;
	StatusBar statusBar;
	JScrollPane transactionScroll;
	DefaultMutableTreeNode top;

	static Cashout cashout;
	static Login loginWin;

	public void update() {
		transactionControls.update();
		 System.out.println("updating gui");
		Controller.loadTransactions();
		addTransactions();
	}

	public GUI() {
		setTitle("Coffee bar");
		setUndecorated(true);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		// make login screen
		loginWin = new Login();
		
		// make cashout screen
		cashout = new Cashout();
		mainArea = new JPanel();
		mainArea.setLayout(new BorderLayout());

		items = new Grid(5, 5);
		users = new Grid(5, 5);

		transactionControls = new TransactionControls();

		transactionListArea = new JPanel();

		top = new DefaultMutableTreeNode("Transactions");
		transactionList = new JTree(top);
		transactionList.setRootVisible(false);

		// transactionList.setCellRenderer(new TransactionCellRenderer());

		transactionList.setBackground(Color.white);
		/*
		 * transactionList.addListSelectionListener(new ListSelectionListener() {
		 * public void valueChanged(ListSelectionEvent e) {
		 * ((JList)e.getSource()).updateUI(); } });
		 */
		transactionScroll = new JScrollPane(transactionList);

		bCashout = new JButton("Cashout");
		bCashout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Controller.cashout();
			}
		});
		transactionListArea.setLayout(new BorderLayout());
		transactionListArea.add(bCashout, BorderLayout.NORTH);
		transactionListArea.add(transactionScroll, BorderLayout.CENTER);

		statusBar = new StatusBar();

		mainArea.add(items, BorderLayout.CENTER);
		mainArea.add(transactionControls, BorderLayout.SOUTH);
		mainArea.add(transactionListArea, BorderLayout.WEST);

		loginWin.setVisible(false);
		cashout.setVisible(false);
		mainArea.setVisible(false);
		//this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
	
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.anchor = GridBagConstraints.PAGE_START;
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		add(loginWin,c);
		add(cashout,c);
		add(mainArea,c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.weightx = 1;
		c.weighty = 0;
		c.anchor = GridBagConstraints.PAGE_END;
		this.add(statusBar,c);

		update();
	}

	public void addItems(LinkedList items) {
		Iterator it = items.iterator();
		while (it.hasNext()) {
			Item item = (Item) it.next();
			this.items.addItem(item);
		}
	}

	public void addUsers(LinkedList users) {
		Iterator it = users.iterator();
		while (it.hasNext()) {
			User user = (User) it.next();
			this.users.addItem(user);
		}
	}

	public void addTransactions() {
		try {
			top.removeAllChildren();
			Iterator it = Controller.pastTransactions.iterator();

			while (it.hasNext()) {
				Transaction t = (Transaction) it.next();
				DefaultMutableTreeNode n = new DefaultMutableTreeNode(t
						.getTime());
				Iterator it1 = t.iterator();
				while (it1.hasNext()) {
					Item item = (Item) it1.next();
					n.add(new DefaultMutableTreeNode(item.getName()));
				}

				top.add(n);
				transactionList.updateUI();
			}
		} catch (NullPointerException ex) {
			System.out.println("add transaction: nothing to add");
		}

	}

	public void showUsers() {
		this.remove(items);
		items.setVisible(false);
		users.setVisible(true);
		transactionControls.free.setText("Back to items");
		mainArea.add(users, BorderLayout.CENTER);
	}

	public void showItems() {
		this.remove(users);
		users.setVisible(false);
		items.setVisible(true);
		transactionControls.free.setText("Discount");
		mainArea.add(items, BorderLayout.CENTER);
	}

	public static void setCORealTotal(double d) {
		cashout.moneyCountingArea.realTotal = d;
	}

	public void showLogoutWin() {
		loginWin.setVisible(false);		
		mainArea.setVisible(false);
		cashout.moneyCountingArea.update();
		cashout.setVisible(true);
	}

	public void setCIExpectedTotal(double d) {
		loginWin.moneyCountingArea.realTotal = d;
		loginWin.moneyCountingArea.update();
	}

	public void showLoginWin() {
		loginWin.setVisible(true);
		loginWin.moneyCountingArea.update();
//		make it the dimensions of the screen
		this.setState(Frame.NORMAL);
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		this.setPreferredSize(toolkit.getScreenSize());
		this.getContentPane().setSize(getPreferredSize());
		this.setLocation(0, 0);
		//this.setResizable(false);
		//this.update();
		
		this.pack();
		setVisible(true);
	}

	public void showMainWin() {
		loginWin.setVisible(false);
		mainArea.setVisible(true);
		loginWin.moneyCountingArea.update();
		
		
		// this.setVisible(true);
		this.pack();
		setVisible(true);
	}

}
